﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Cit.CDE.Net.Service.ModuleFactory;
using Cit.CDE.Net.Module;
using System.Collections.Generic;
using Cit.CDE.Net.Module.Commons;
using cde.SystemCommon;
using Cit.CDE.Net.Service;
using System.Text;

using Module;


/// <summary>
/// Copyright (C)   jinggubang
/// FileName:        ActivityView.aspx.cs           
/// Author :         wangbc
/// CDT:             2012-02-01           
/// Version:         1.0.0.0                
/// Depiction:       市场活动新增    
/// </summary>
public partial class main_sales_ActivityView : UserControl.CommonPage
{
    /// <summary>
    /// 市场活动EM
    /// </summary>
    private CDEEntityManager em;
    /// <summary>
    /// 市场活动Entity
    /// </summary>
    private CDEEntity m_entity;
    /// <summary>
    /// 市场活动Entity名字
    /// </summary>    
    private const string EM_FULL_NAME = WEBConstants.EM_MARKET_ACTIVITY_MANAGE;
    //请求
    public string action;

    /// <summary>
    /// 消息来源ID
    /// </summary>
    public string SOURCE_ID = string.Empty;

    /// <summary>
    /// 页面初始化
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        //市场活动ID
        string chkID = Util.objectToString(Request.QueryString.Get("chkID"));
        //操作请求
        action = Util.objectToString(Request["action"]);
        if (action.Equals("request"))
        {
            this.tr_status.Style.Value= "display:none";
            this.tr_report.Style.Value = "display:none";
        } if (action.Equals("report"))
        {
            this.tr_status.Style.Value = "display:none";        
        }

        em = CDEEMFactory.getEMByFullName(EM_FULL_NAME);
        if (!Page.IsPostBack)
        {
            

            //ID不为空修改市场活动，否则新增市场活动
            if (chkID != "" && chkID != null)
            {            
                //修改
                this.page_title.InnerText = "市场活动修改";
                CDESearchCriteria sc = new CDESearchCriteria(em);
                sc.addCondition("ID", chkID);
                List<CDEEntity> listEntity = CDEService.LoadEntities(em, sc);
                //市场活动ID是否存在
                if (listEntity.Count > 0)
                {
                    m_entity = listEntity[0];
                    m_entity.Operation = CDEDataOperation.UPDATE;
 
                }
                else
                {
                    m_entity = CDEEMServiceFactory.getEMService(em).CreateEntity();
                    m_entity.Operation = CDEDataOperation.INSERT;
                }

                //可以发送通知
                SOURCE_ID = m_entity.Id;
                
            }
            else
            {
                //新增时不显示
               // this.tr_status.Visible = false;
               // this.tr_report.Visible = false;


                //新增
                this.page_title.InnerText = "市场活动新增";
                m_entity = CDEEMServiceFactory.getEMService(em).CreateEntity();
                m_entity.Operation = CDEDataOperation.INSERT;
               
            }
            Session["MENTITY"] = m_entity;
            //市场活动信息设值到页面
            this.setControls(m_entity, Resources.ResourceErrMsg.ResourceManager);
        }
        else
        {
            m_entity = (CDEEntity)Session["MENTITY"];
            //页面信息放入市场活动Entity
            this.setEntityFromRequest(m_entity, Request.Params);
        }
    }
    /// <summary>
    /// 确定按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void queding_Click(object sender, EventArgs e)
    {
        //修改
        string chkID = Util.objectToString(Request.QueryString.Get("chkID"));
        if (chkID != "") {
            m_entity.Operation = CDEDataOperation.UPDATE;
        }


        //当前登陆人信息
        LoginOprator op = (LoginOprator)Session[Cit.CDE.Net.Module.Commons.CDEConstants.SESSION_LOIN_OPRATOR];
        //当前时间
        string Now = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Replace("-", "/");
        if (m_entity.Operation == CDEDataOperation.INSERT)
        {
            m_entity.set("INSERT_USER", op.SysID);
            m_entity.set("INSERT_DATE", Now);
        }
        else
        {
            m_entity.set("UPDATE_USER", op.SysID);
            m_entity.set("UPDATE_DATE", Now);
        }

  

        List<CDEEntity> listEntity = new List<CDEEntity>();

        listEntity.Add(m_entity);

        bool isSaveError = false;

        CDEService.UpdateEntities(em, listEntity);

        if (listEntity[0].Result == CDEDataOperationResult.ERROR)
        {
            isSaveError = true;
        }
        else
        {
            isSaveError = false;
        }

        //成功返回列表，失败提示错误信息
        if (isSaveError)
        {
            showMessageErrAndOk(Resources.ResourceErrMsg.SAVE_ERR);
            this.setControls(m_entity, Resources.ResourceErrMsg.ResourceManager);
        }
        else
        {
            
            StringBuilder sbr = new StringBuilder();
            sbr.AppendFormat("<script language=\"javascript\">{0}", Environment.NewLine);
            sbr.AppendFormat("alert(\"");
            sbr.AppendFormat(Resources.Resource.RESULT_OK);
           // sbr.AppendFormat("\");</script>{0}", Environment.NewLine);
            sbr.AppendFormat("\");window.location='../sales/ActivityList.aspx?EM=" + EM_FULL_NAME + "&sel_menu_id=" + Sel_MenuNode.Attributes["id"].Value + "&IsBackSc=1&action="+action+"';</script>{0}", Environment.NewLine);
            Page.ClientScript.RegisterStartupScript(Page.GetType(), "Startup", sbr.ToString());
        }

    }
    /// <summary>
    /// 返回按钮
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void cmdCancel_Click(object sender, EventArgs e)
    {
        if (Sel_MenuNode == null)
            return;
        StringBuilder url = new StringBuilder();
        url.Append("../sales/ActivityList.aspx?EM=");
        url.Append(EM_FULL_NAME);
        url.Append("&sel_menu_id=");
        url.Append(Sel_MenuNode.Attributes["id"].Value);
        url.Append("&IsBackSc=1");
        url.AppendFormat("&action={0}", action);
        Response.Redirect(url.ToString()); 
    } 
}
